/*
 * Copyright 2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.pajamas.starter.integration.module.client;

import org.pajamas.starter.core.util.RegisterAsPajamasClient;
import org.pajamas.starter.integration.spring.amqp.DeadLetterListener;
import org.pajamas.starter.integration.spring.amqp.DeadLetterPolicy;
import org.pajamas.starter.integration.spring.amqp.Declarables;
import org.pajamas.starter.integration.spring.amqp.LoggingDeadLetterPolicy;
import org.pajamas.starter.integration.spring.amqp.PublishEndpoint;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;

/**
 * @author william
 * @since 2024/5/14
 */
@ConditionalOnClass(AmqpTemplate.class)
@EnableRabbit
@RegisterAsPajamasClient.ConfigurationBean
public class PajamasQueueClientConfiguration {
    @ConditionalOnProperty(prefix = "org.pajamas.starter.client.queue", value = "converter",
            havingValue = "true", matchIfMissing = true)
    @ConditionalOnMissingBean
    @Bean
    public MessageConverter pajamasMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @ConditionalOnProperty(prefix = "org.pajamas.starter.client.queue", value = "publishEndpoint",
            havingValue = "true", matchIfMissing = true)
    @ConditionalOnMissingBean
    @Bean
    public PublishEndpoint pajamasPublishEndpoint(RabbitTemplate template) {
        return new PublishEndpoint(template);
    }

    @ConditionalOnProperty(prefix = "org.pajamas.starter.client.queue", value = "dlx", havingValue = "true",
            matchIfMissing = true)
    @ConditionalOnMissingBean(value = Exchange.class, name = "pajamasDeadLetterExchange")
    @Bean
    public Exchange pajamasDeadLetterExchange() {
        return Declarables.DLX;
    }

    @ConditionalOnBean(value = Exchange.class, name = "pajamasDeadLetterExchange")
    @ConditionalOnMissingBean(value = Queue.class, name = "pajamasDeadLetterQueue")
    @Bean
    public Queue pajamasDeadLetterQueue() {
        return Declarables.DLQ;
    }

    @ConditionalOnBean(value = Queue.class, name = "pajamasDeadLetterQueue")
    @ConditionalOnMissingBean(value = Binding.class, name = "pajamasDeadLetterBinding")
    @Bean
    public Binding pajamasDeadLetterBinding() {
        return Declarables.DLB;
    }

    @ConditionalOnProperty(prefix = "org.pajamas.starter.client.queue",
            value = "loggingDeadLetterPolicy", havingValue = "true", matchIfMissing = true)
    @ConditionalOnMissingBean
    @Bean
    public LoggingDeadLetterPolicy pajamasLoggingDeadLetterPolicy() {
        return new LoggingDeadLetterPolicy();
    }

    @ConditionalOnBean(value = Binding.class, name = "pajamasDeadLetterBinding")
    @ConditionalOnMissingBean
    @Bean
    public DeadLetterListener pajamasDeadLetterListener(ObjectProvider<DeadLetterPolicy> policies) {
        return new DeadLetterListener(policies.orderedStream().iterator());
    }
}